package com.example.myapplication3;

import android.nfc.Tag;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class TwoScoringActivity extends AppCompatActivity {
    int scoreTeamA = 0;
    int scoreTeamB = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            setContentView(R.layout.activity_main);
        } catch (Exception e) {
            showToast("初始化界面失败: " + e.getMessage());
            finish();
        }
    }

    // 处理加分按钮点击
    public void addPoints(View view) {
        try {
            int pointsToAdd = 0;
            String toastMessage = "";
            boolean isTeamA = false;

            // 使用 if-else 替代 switch 处理资源 ID
            int viewId = view.getId();
            if (viewId == R.id.team_a_3_points) {
                pointsToAdd = 3;
                toastMessage = "队伍A +3分";
                isTeamA = true;
            } else if (viewId == R.id.team_a_2_points) {
                pointsToAdd = 2;
                toastMessage = "队伍A +2分";
                isTeamA = true;
            } else if (viewId == R.id.team_a_free_throw) {
                pointsToAdd = 1;
                toastMessage = "队伍A +1分";
                isTeamA = true;
            } else if (viewId == R.id.team_b_3_points) {
                pointsToAdd = 3;
                toastMessage = "队伍B +3分";
            } else if (viewId == R.id.team_b_2_points) {
                pointsToAdd = 2;
                toastMessage = "队伍B +2分";
            } else if (viewId == R.id.team_b_free_throw) {
                pointsToAdd = 1;
                toastMessage = "队伍B +1分";
            }

            // 更新分数
            if (isTeamA) {
                scoreTeamA += pointsToAdd;
                updateTeamAScore();
            } else {
                scoreTeamB += pointsToAdd;
                updateTeamBScore();
            }

            showToast(toastMessage);

        } catch (Exception e) {
            showToast("处理分数时出错: " + e.getMessage());
        }
    }

    // 处理重置按钮点击
    public void resetScores(View view) {
        try {
            scoreTeamA = 0;
            scoreTeamB = 0;
            updateTeamAScore();
            updateTeamBScore();
            showToast("比分已重置");
        } catch (Exception e) {
            showToast("重置分数时出错: " + e.getMessage());
        }
    }

    private void updateTeamAScore() {
        try {
            TextView scoreView = findViewById(R.id.team_a_score);
            scoreView.setText(String.valueOf(scoreTeamA));
        } catch (Exception e) {
            showToast("更新队伍A分数失败");
        }
    }

    private void updateTeamBScore() {
        try {
            TextView scoreView = findViewById(R.id.team_b_score);
            scoreView.setText(String.valueOf(scoreTeamB));
        } catch (Exception e) {
            showToast("更新队伍B分数失败");
        }
    }

    private void showToast(String message) {
        try {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("teama_score",scoreTeamA);
        outState.putInt("teamb_score",scoreTeamB);


    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        scoreTeamA= savedInstanceState.getInt("teama_score");
        scoreTeamB= savedInstanceState.getInt("teamb_score");
        updateTeamAScore();
        updateTeamBScore();


    }
}